﻿using System;
using System.Web;

using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Web.SessionState;

namespace IFD2
{
    public class avail_open_fares : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            //context.Response.ContentType = "text/plain";
            //context.Response.Write("Hello World");

            //if (IsPostBack)
            //{
            //    Response.Write("* Don't post back!");
            //    Response.End();
            //    return;
            //}

            if (context.Session["ACL"].ToString().IndexOf("SA3") < 0)
            {
                context.Response.Write("* Unauthorized!");
                context.Response.End();
                return;
            }
            //Response.Write("OK");
            //Response.End();
            //return;
            SqlCommand cmd = new SqlCommand();
            SqlParameter para;
            StringBuilder buf = new StringBuilder();
            int ret_code, i;
            DataTable dt_ret = new DataTable();
            i = Convert.ToInt16(context.Request.QueryString["seg_sn"]);
            //context.Session["FLT_DATE_" + i.ToString()] = Convert.ToDateTime(context.Session["FLT_DATE_" + i.ToString()]).AddDays(Convert.ToSingle(context.Request.QueryString["offset"]));
            try
            {
                cmd.Parameters.Clear();
                cmd.CommandText = "bll_query_get_avail_open_fares";
                para = cmd.Parameters.AddWithValue("@ori_city", context.Session["ORI_" + i.ToString()]);
                para = cmd.Parameters.AddWithValue("@des_city", context.Session["DES_" + i.ToString()]);
                para = cmd.Parameters.AddWithValue("@lang", context.Session["LANG"].ToString());
                //para = cmd.Parameters.AddWithValue("@flt_date", Convert.ToDateTime(context.Session["FLT_DATE_" + i.ToString()]));
                para = cmd.Parameters.AddWithValue("@seats", Convert.ToInt16(context.Session["SEATS"]));
                para = cmd.Parameters.AddWithValue("@seg_sn", i);
                para = cmd.Parameters.AddWithValue("@seg_total", Convert.ToInt16(context.Session["TRIP_COUNT"]));
                para = cmd.Parameters.AddWithValue("@user_airline", context.Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@user_office", context.Session["OFFICE"].ToString());
                para = cmd.Parameters.AddWithValue("@user", context.Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", context.Request.UserHostAddress.ToString());

                ret_code = TinyDAL.Exec(ref cmd, ref dt_ret);
                if (ret_code != 0)
                {
                    if (ret_code == 1)
                    {
                        context.Response.Write("City not found!");
                        context.ApplicationInstance.CompleteRequest();
                        return;
                    }
                    //else if (ret_code == 2)
                    //{
                    //    context.Response.Write("Daylight saving time error!");
                    //    context.ApplicationInstance.CompleteRequest();
                    //    return;
                    //}
                    else
                    {
                        context.Response.Write("Error " + ret_code.ToString());
                        context.ApplicationInstance.CompleteRequest();
                        return;
                    }
                }
                String fare_key;
                int row_count = 0;
                buf.Append("<b>" + i.ToString() + ". " + context.Session["ORI_" + i.ToString()].ToString() + "-" + context.Session["DES_" + i.ToString()].ToString() + "</b><br/>");
                if (dt_ret.Rows.Count == 0)
                {
                    buf.Append("No available product.<br/>");
                }
                else
                {
                    foreach (DataRow curr_row in dt_ret.Rows)
                    {
                        row_count++;

                        fare_key = "AIRLINE=" + curr_row["airline"].ToString().TrimEnd() + "&FARE_CODE=" + curr_row["fare_code"].ToString().TrimEnd()
                        + "&ORI_AIRPORT=" + curr_row["ori_airport"].ToString().TrimEnd() + "&DES_AIRPORT=" + curr_row["des_airport"].ToString().TrimEnd() + "&SEG_SN=" + curr_row["seg_sn"].ToString().TrimEnd() + "&FLT_SURCHARGE_TYPE=0"
                        + "&LANG=" + context.Session["LANG"].ToString().TrimEnd();

                        //buf.Append("<table><tr><td width='10px'></td><td><input type='radio' onclick=\"AjaxGetFareDetails(event,'OpenFareDetails.aspx?" + fare_key + "')\"
                        buf.Append("<table style=\"min-width:1000px;\"><tr><td width='10px'></td><td><input type='radio' onclick=\"$('#ajax1').load('OpenFareDetails.aspx?" + fare_key + "');$('#modXXX').modal('show');\"");
                        buf.Append(" name='KEY" + context.Request.QueryString["seg_sn"] + "' value='2000-1-1|"
                            + curr_row["airline"].ToString().Trim() + "|OPEN|" + curr_row["ori_airport"].ToString() + "|" + curr_row["des_airport"].ToString() + "|" + curr_row["cabin"].ToString() + "|" + curr_row["class"] + "|" + curr_row["fare_code"] + "|" + curr_row["dbf"].ToString() + "|2000-1-1|2000-1-1'></input></td><td> ");
                        buf.Append(curr_row["airline_name"].ToString().Trim() + "&nbsp;&nbsp;&nbsp;</td><td>" + curr_row["cabin_name"].ToString() + "&nbsp;&nbsp;&nbsp;</td><td><b>");
                        if (Convert.ToInt16(curr_row["seg_total"]) > 1)
                            buf.Append("<span style=\"background:#90f090;\">");
                        buf.Append(curr_row["fare_name"].ToString() + "(" + curr_row["fare_code"].ToString() + ")");
                        if (Convert.ToInt16(curr_row["seg_total"]) > 1)
                            buf.Append("</span>");
                        buf.Append("</b>&nbsp;&nbsp;&nbsp;</td><td><b>" + curr_row["currency"].ToString() + Convert.ToSingle(curr_row["adt_price"]).ToString() + "/" + Convert.ToSingle(curr_row["chd_price"]).ToString() + "/" + Convert.ToSingle(curr_row["inf_price"]).ToString()
                            + "</b>&nbsp;&nbsp;&nbsp;</td><td>" + curr_row["ori_apt_name"].ToString().TrimEnd() + "-" + curr_row["des_apt_name"].ToString() + "(" + curr_row["seg_sn"] + "/" + curr_row["seg_total"]
                            + ")&nbsp;&nbsp;&nbsp;</td><td>" /*DBF= + curr_row["dbf"].ToString() + "&nbsp;&nbsp;&nbsp;</td><td>"*/ + Convert.ToDateTime(curr_row["fltdate_from"]).ToShortDateString() + "~" + Convert.ToDateTime(curr_row["fltdate_to"]).ToShortDateString()
                            + "&nbsp;&nbsp;&nbsp;</td><td>BOOK in advance:<br/>" + curr_row["open_advance"].ToString() + " ~ " + curr_row["close_advance"].ToString() + "&nbsp;&nbsp;&nbsp;</td></tr></table>");

                        buf.Append("<div id=\"fare_" + i.ToString() + "_" + row_count.ToString() + "\"></div><br/>");
                    }
                }
                dt_ret.Clear();
                buf.Append("<hr/>");
                context.Response.Write(buf.ToString());
                context.ApplicationInstance.CompleteRequest();
            }
            catch (Exception ex)
            {
                if (context.Application["DEBUG"].ToString() == "1")
                {
                    context.Response.Write(ex.Message);
                    context.ApplicationInstance.CompleteRequest();
                }
                else
                {
                    context.Response.Write("Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.");
                    context.ApplicationInstance.CompleteRequest();
                }
                return;
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

    }
}